package com.prawn.ift_collequipment.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.prawn.ift_collequipment.dao.EquipmentDao;
import com.prawn.ift_collequipment.pojo.Equipment;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class EquipmentService {

	@Autowired
	private EquipmentDao equipmentDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Equipment> findAll() {
		return equipmentDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Equipment> findSearch(Map whereMap, int page, int size) {
		Specification<Equipment> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return equipmentDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Equipment> findSearch(Map whereMap) {
		Specification<Equipment> specification = createSpecification(whereMap);
		return equipmentDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	@Cacheable(value = "equipment",key = "#id")
	public Equipment findById(String id) {
		return equipmentDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param equipment
	 */
	public void add(Equipment equipment) {
		equipment.setId( idWorker.nextId()+"" );
		equipmentDao.save(equipment);
	}

	/**
	 * 修改
	 * @param equipment
	 */
	@CacheEvict(value = "equipment",key = "#equipment.id")
	public void update(Equipment equipment) {
		equipmentDao.save(equipment);
	}

	/**
	 * 删除
	 * @param id
	 */
	@Cacheable(value = "equipment",key = "#id")
	public void deleteById(String id) {
		equipmentDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Equipment> createSpecification(Map searchMap) {

		return new Specification<Equipment>() {

			@Override
			public Predicate toPredicate(Root<Equipment> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 设备编码
                if (searchMap.get("number")!=null && !"".equals(searchMap.get("number"))) {
                	predicateList.add(cb.like(root.get("number").as(String.class), "%"+(String)searchMap.get("number")+"%"));
                }
                // 名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 设备序列号
                if (searchMap.get("serial")!=null && !"".equals(searchMap.get("serial"))) {
                	predicateList.add(cb.like(root.get("serial").as(String.class), "%"+(String)searchMap.get("serial")+"%"));
                }
                // 设备类型编号
                if (searchMap.get("typeId")!=null && !"".equals(searchMap.get("typeId"))) {
                	predicateList.add(cb.like(root.get("typeId").as(String.class), "%"+(String)searchMap.get("typeId")+"%"));
                }
                // 场景编号
                if (searchMap.get("sceneId")!=null && !"".equals(searchMap.get("sceneId"))) {
                	predicateList.add(cb.like(root.get("sceneId").as(String.class), "%"+(String)searchMap.get("sceneId")+"%"));
                }
                // 设备型号
                if (searchMap.get("model")!=null && !"".equals(searchMap.get("model"))) {
                	predicateList.add(cb.like(root.get("model").as(String.class), "%"+(String)searchMap.get("model")+"%"));
                }
                // 供电方式,引用数据字典
                if (searchMap.get("powerType")!=null && !"".equals(searchMap.get("powerType"))) {
                	predicateList.add(cb.like(root.get("powerType").as(String.class), "%"+(String)searchMap.get("powerType")+"%"));
                }
                // 移动设备身份码IMSI
                if (searchMap.get("imsiCard")!=null && !"".equals(searchMap.get("imsiCard"))) {
                	predicateList.add(cb.like(root.get("imsiCard").as(String.class), "%"+(String)searchMap.get("imsiCard")+"%"));
                }
                // 设备厂家
                if (searchMap.get("manufactorId")!=null && !"".equals(searchMap.get("manufactorId"))) {
                	predicateList.add(cb.like(root.get("manufactorId").as(String.class), "%"+(String)searchMap.get("manufactorId")+"%"));
                }
                // 设备状态，引用数据字典
                if (searchMap.get("equipStatus")!=null && !"".equals(searchMap.get("equipStatus"))) {
                	predicateList.add(cb.like(root.get("equipStatus").as(String.class), "%"+(String)searchMap.get("equipStatus")+"%"));
                }
                // 关联设备
                if (searchMap.get("associatedDevice")!=null && !"".equals(searchMap.get("associatedDevice"))) {
                	predicateList.add(cb.like(root.get("associatedDevice").as(String.class), "%"+(String)searchMap.get("associatedDevice")+"%"));
                }
                // 备注信息
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.like(root.get("createBy").as(String.class), "%"+(String)searchMap.get("createBy")+"%"));
                }
                // 更新者
                if (searchMap.get("updateBy")!=null && !"".equals(searchMap.get("updateBy"))) {
                	predicateList.add(cb.like(root.get("updateBy").as(String.class), "%"+(String)searchMap.get("updateBy")+"%"));
                }
                // 删除标记
                if (searchMap.get("delFlag")!=null && !"".equals(searchMap.get("delFlag"))) {
                	predicateList.add(cb.like(root.get("delFlag").as(String.class), "%"+(String)searchMap.get("delFlag")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}


}
